<?php
	
	include_once('db_mysql.class.php');
	/*
		weijingtai by aming
	*/
	class url_rewrite /*extends db_mysql*/{
		
		var $prefix = '.html';
		var $status = true;
		var $request_url;
		
		
		function __construct()
		{
			$this->prefix = $prefix;
			$this->status = $status;
			//$this->request_url = $_GET["html"];
			
			$this->set();
			//$this->get();
		}
		
		/*public function url_rewrite()
		{
			$this->__construct();
		}*/
		
		function set(){
			$request_url = $_GET['html'];
			
			$pos=strrpos($request_url, '?');
			$url = $pos === false ? $request_url : substr($request_url, $pos+1); 
			$this->url = ltrim($url); 
		}
		
		function revert(){
			
			$request_args = explode("/",ltrim($this->url));
			$request_args = array_diff($request_args, array(null));
			//$request = preg_replace('/(\d+)/','%',$this->url);
			
			
			$args = explode('/',$this->url);
			$args = array_diff($args, array(null));
			//$url = preg_replace('/(\d+)/','%',$this->url);
		 
			$func = $args[0];
			
			// 二级域名
			if(SECONDDOMAIN_ENABLE){
				if(strpos($_SERVER['SERVER_NAME'],'.'.SECONDDOMAIN_NAME) && strpos($_SERVER['SERVER_NAME'],'www.') === false){
					/*if($this->url)*/ $func = 'yellowpage';
					 
				}
			}
			return method_exists($this,$func) ? $this->$func(array('args'=>$args,'url'=>$this->url,'revert'=>1)) : '?'.$this->url;
		}
		
		function rewrite($url){
			//if($this->apache_is_mod_rewrite() == false) return $url;
			
			if($this->status == false) return 'index.php'.str_replace('index.php','',$url);
			if(strstr($url,$this->prefix)) return $url;
			
			$pos=strrpos($url, '?');
			$url = $pos === false ? $url : substr($url, $pos+1); 
			
			
			// 拆分
			$a1 = explode('&',$url);
			
			$args = array();
			foreach($a1 as $k => $v){
				$a2 = explode('=',$v);
				$args[$a2[0]] = $a2[1];
			}
			
			parse_str($url);
			
			/*if($module){
				$out = ;
				global $SETTING;
				return $out;
				return  ? $out : $SETTING['siteurl'].$out;
			}
			else{*/
			
			
			
			if(method_exists($this,$module)){
				global $SETTING;
				$out = $this->$module(array('args'=>$args,'url'=>$url,'rewrite'=>1));
				return strpos($out,'http://') !== false ? $out : $SETTING['siteurl'].$out;
			}
			else{
				return '?'.$url;
			}
			
			
		}
		
		function blog($a){
			extract($a);
			
			if($revert){
				if($blogid = $this->preg("blog/%/edit/",$url)){
					return  "module=blog&action=edit&blogid=$blogid";
				}
				else if($blogid = $this->preg("blog/%/",$url)){
					if(is_numeric($blogid)){
						return  "module=blog&blogid=$blogid";
					}
					else{
						return $this->other($a);
					}
				}
				else{
					return $this->other($a);
				}
			}
			else{
				
			
				if($blogid = $this->preg("module=blog&blogid=%",$url)){
					
					return "blog/$blogid/";
				}
				/*if(strpos($new, 'module=blog&action=edit&blogid=%') !== false){
					
					return "blog/$blogid/edit/";
				}*/
				else if($blogid = $this->preg("module=blog&action=edit&blogid=%",$url)){ 
				
					return "blog/$blogid/edit/";
				}
				else{
					return $this->other($a);
				}
			}
			
		}
		
		function goods($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function category($a){
			extract($a);
		
			if($revert){
			
				if($catid = $this->preg('category/%',$url)){
					
					if(is_numeric($catid)){
						return "module=category&catid=$catid";
					}
					else{
						$url = str_replace('category/','',$url);
						$catdir = $args[count($args)-1];
						$parentdir = substr($url,0,-strlen($catdir)-2);
						return "module=category&catdir=$catdir&parentdir=$parentdir";
					}
				}
				else if($this->preg('category/',$url)){
					return "module=category";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($catid = $this->preg('module=category&action=view&catid=%',$url)){
					return "category/$catid/";
				}
				else if($this->preg('module=category&catdir=',$url)){
					return $parentdir ? "category/$parentdir/$catdir/" : "category/$catdir/";
				}
				else{
					return $this->other($a);
				}
			}
			
		}
		function content($a){
			extract($a);
			if($revert){
			
				if($contentid = $this->preg("content/%/edit",$url)){
					return "module=content&action=edit&contentid=$contentid";
				}
				else if($contentid = $this->preg("content/%/delete",$url)){
					return "module=content&action=delete&contentid=$contentid";
				}
				else if($tag = $this->preg('content/tag/',$url)){
					return "module=content&action=tag&tag=$tag";
				}
				else if($contentid = $this->preg('content/%/',$url)){
					
					if(is_numeric($contentid)){
						return "module=content&contentid=$contentid";
					}
					else{
						return $this->other($a);
					}
					
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($contentid = $this->preg('module=content&contentid=%',$url)){
					return "content/$contentid/";
				}
				if($contentid = $this->preg('module=content&action=view&contentid=%',$url)){
					return "content/$contentid/";
				}
				else if($contentid = $this->preg("module=content&action=edit&contentid=%",$url)){
					return "content/$contentid/edit";
				}
				else if($contentid = $this->preg("module=content&action=delete&contentid=%",$url)){
					return "content/$contentid/delete";
				}
				else if($name = $this->preg('module=content&action=tag&name=',$url)){
					return "content/tag/$name/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function area($a){
			extract($a);
			if($revert){
			
				/*if($name = $this->preg('area/',$url)){
					return "module=area&name=$name";
				}*/
				if($areaid = $this->preg('area/%/',$url)){
					
					if(is_numeric($areaid)){
						return "module=area&areaid=$areaid";
					}
					else{
						return "module=area&dir=$areaid";
					}
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($areaid = $this->preg('module=area&areaid=%',$url)){
					return "area/$areaid/";
				}
				else if($dir = $this->preg('module=area&dir=%',$url)){
					return "area/$dir/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function ask($a){
			extract($a);
			if($revert){
			
				if($id = $this->preg('ask/%',$url)){
					return "module=ask&action=view&id=$id";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($id = $this->preg('module=ask&action=view&id=%',$url)){
					return "ask/$id/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function codex($a){
			extract($a);
			if($revert){
			
				if($codexid = $this->preg('codex/%',$url)){
					return "module=codex&action=view&codexid=$codexid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($codexid = $this->preg('module=codex&codexid=%',$url)){
					return "codex/$codexid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function space($a){
			extract($a);
			if($revert){
			
				if($userid = $this->preg('space/%/blog',$url)){
					return is_numeric($userid) ? "module=space&action=blog&userid=$userid" : "module=space&action=blog&prefix=$userid";
				}
				else if($userid = $this->preg('space/%/profile',$url)){
					return is_numeric($userid) ? "module=space&action=profile&userid=$userid" : "module=space&action=profile&prefix=$userid";
				}
				else if($userid = $this->preg('space/%/tweet',$url)){
					return is_numeric($userid) ? "module=space&action=tweet&userid=$userid" : "module=space&action=tweet&prefix=$userid";
				}
				else if($userid = $this->preg('space/%/collect',$url)){
					return is_numeric($userid) ? "module=space&action=collect&userid=$userid" : "module=space&action=collect&prefix=$userid";
				}
				else if($userid = $this->preg('space/%/contribute',$url)){
					return is_numeric($userid) ? "module=space&action=contribute&userid=$userid" : "module=space&action=contribute&prefix=$userid";
				}/*
				else if($userid = $this->preg('space/%',$url)){
					
					echo $userid;
					if($args[2] && !in_array($args[1],array('blog','profile','tweet','collect'))){
					
						global $db;
						$r = $db->get_one("select `userid` from ".DB_PRE."member where `prefix` = '$args[1]'");
						if($r){
							return "module=space&action=$args[2]&prefix=$args[1]";
						}
						else{
							return "space/$userid/";
						}	
					}
					else{
					
						global $db;
						$r = $db->get_one("select `userid` from ".DB_PRE."member where `prefix` = '$args[1]'");
						if($r){
							return "module=space&prefix=$args[1]";
						}
						else{
							return "space/$userid/";
						}
					}
					
				}*/
				else if($userid = $this->preg('space/%/',$url)){
					return is_numeric($userid) ? "module=space&userid=$userid" : "module=space&prefix=$userid";
				}
			}
			else{
				if($userid = $this->preg("module=space&userid=%",$url)){
					
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/";
					}
					else{
						return "space/$userid/";
					}
				}
				else if($userid = $this->preg("module=space&action=blog&userid=%",$url)){
			
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/blog";
					}
					else{
						return "space/$userid/blog";
					}
				}
				else if($userid = $this->preg("module=space&action=profile&userid=%",$url)){
					
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/profile";
					}
					else{
						return "space/$userid/profile";
					}
				}
				else if($userid = $this->preg("module=space&action=tweet&userid=%",$url)){
					
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/tweet";
					}
					else{
						return "space/$userid/tweet";
					}
				}
				else if($userid = $this->preg("module=space&action=collect&userid=%",$url)){
					
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/collect";
					}
					else{
						return "space/$userid/collect";
					}
				}
				else if($userid = $this->preg("module=space&action=contribute&userid=%",$url)){
					
					global $db,$_userid;
					$userid = $userid ? $userid : $_userid;
					
					$r = $db->get_one("select `prefix` from table_member where `userid` = $userid");
					if($r['prefix'] != ''){
						return "space/$r[prefix]/contribute";
					}
					else{
						return "space/$userid/contribute";
					}
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function member($a){
			extract($a);
			if($revert){
			
				if($action = $this->preg('member/connect/%/',$url)){
					return "module=member&file=connect&action=$action";
				}
				else if($this->preg('member/connect/',$url)){
					return "module=member&file=connect";
				}
				else if($userid = $this->preg('member/%/',$url)){
					return is_numeric($userid) ? "module=member&action=view&userid=$userid" : "module=member&action=$userid";
				}
				else if($this->preg('member/manage/',$url)){
					return "module=member&file=manage/";
				}
				else if($this->preg('member/collect/',$url)){
					return "module=collect";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($userid = $this->preg('module=member&action=view&userid=%',$url)){
					return "member/$userid/";
				}
				if($action = $this->preg('module=member&file=connect&action=%',$url)){
					return "member/connect/$action/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function announce($a){
			extract($a);
			if($revert){
				
				if($announceid = $this->preg('announce/%',$url)){
					return "module=announce&announceid=$announceid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($announceid = $this->preg('module=announce&announceid=%',$url)){
					return "announce/$announceid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function taobao($a){
			extract($a);
			if($revert){
				
				if($id = $this->preg('taobao/%',$url)){
					return "module=taobao&id=$id";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($id = $this->preg('module=taobao&id=%',$url)){
					return "taobao/$id/";
				}
				else{
					return $this->other($a);
				}
			}
			
		}
		function yellowpage($a){
			extract($a);
			
			if($revert){
				
			    // 二级域名
				if(SECONDDOMAIN_ENABLE){
					if($this->preg('%.'.SECONDDOMAIN_NAME,$_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'],'www.') === false){
					
						$sitedomain = $this->preg('%.'.SECONDDOMAIN_NAME,$_SERVER['SERVER_NAME']);
					
						global $db;
						$result = $db->get_one("select `userid` from ".DB_PRE."member_company where sitedomain = '$sitedomain'");
						
						if(!$result){
							showmessage('很抱歉，您要访问的页面不存在！<br/>1、请检查您输入的网址是否正确。<br/>2、您可以通过网站首页进行浏览。<br/>3、感谢您使用本站，如有疑问请联系我们。');
						}
						$userid = $result['userid'];
						
						if($action = $this->preg('%/',$url)){
							
							global $SETTING;
							return "module=yellowpage&action=$action&userid=$userid";
						}
						else{
							return "module=yellowpage&userid=$userid";
						}
					}
				}
				
				
			
				if($userid = $this->preg('yellowpage/%/introduce/',$url)){
					return "module=yellowpage&action=introduce&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/news/',$url)){
					return "module=yellowpage&action=news&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/product/',$url)){
					return "module=yellowpage&action=product&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/buy/',$url)){
					return "module=yellowpage&action=buy&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/job/',$url)){
					return "module=yellowpage&action=job&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/certificate/',$url)){
					return "module=yellowpage&action=certificate&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/guestbook/',$url)){
					return "module=yellowpage&action=guestbook&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/contact/',$url)){
					return "module=yellowpage&action=contact&userid=$userid";
				}
				else if($userid = $this->preg('yellowpage/%/contact/',$url)){
					return "module=yellowpage&action=contact&userid=$userid";
				}
				else if($id = $this->preg('yellowpage/product/%/',$url)){
					return "module=yellowpage&file=product&action=show&id=$id";
				}
				else if($this->preg('yellowpage/company/',$url)){
					return "module=yellowpage&file=company";
				}
				else if($this->preg('yellowpage/product/',$url)){
					return "module=yellowpage&file=product";
				}
				else if($id = $this->preg('yellowpage/buy/%/',$url)){
					return "module=yellowpage&file=buy&action=show&id=$id";
				}
				else if($this->preg('yellowpage/buy/',$url)){
					return "module=yellowpage&file=buy";
				}
				else if($id = $this->preg('yellowpage/news/%/',$url)){
					return "module=yellowpage&file=news&action=show&id=$id";
				}
				else if($this->preg('yellowpage/news/',$url)){
					return "module=yellowpage&file=news";
				}
				else if($id = $this->preg('yellowpage/job/%/',$url)){
					return "module=yellowpage&file=job&action=show&id=$id";
				}
				else if($this->preg('yellowpage/job/',$url)){
					return "module=yellowpage&file=job";
				}
				else if($this->preg('yellowpage/apply/',$url)){
					return "module=yellowpage&file=apply";
				}
				else if($this->preg('yellowpage/myjob/edit/',$url)){
					return "module=yellowpage&file=myjob&action=edit";
				}
				else if($this->preg('yellowpage/myjob/add/',$url)){
					return "module=yellowpage&file=myjob&action=add";
				}
				else if($this->preg('yellowpage/myjob/',$url)){
					return "module=yellowpage&file=myjob";
				}
				else if($id = $this->preg('yellowpage/%/',$url)){
					return is_numeric($id) ? "module=yellowpage&id=$id" : "module=yellowpage&action=$id";
				}
				else if($this->preg('yellowpage/',$url)){
					return "module=yellowpage";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				
				extract($a);
				$userid = intval($args['userid']);
				
				if(SECONDDOMAIN_ENABLE && $userid){
					// 二级域名
					global $db;
					$result = $db->get_one("select `sitedomain` from ".DB_PRE."member_company where userid = $userid");
					$sitedomain = $result['sitedomain'];
	
					if($sitedomain){
						if($userid = $this->preg('module=yellowpage&userid=%',$url)){
							return 'http://'.$sitedomain.'.'.SECONDDOMAIN_NAME;
						}
						else if($action = $this->preg("module=yellowpage&action=%&userid=%",$url)){
							
							return 'http://'.$sitedomain.'.'.SECONDDOMAIN_NAME.'/'.$action.'/';
						}
					}
				}
				
				
				
				if($userid = $this->preg('module=yellowpage&userid=%',$url)){
					return "yellowpage/$userid/";
				}
				else if($userid = $this->preg('module=yellowpage&action=introduce&userid=%',$url)){
					return "yellowpage/$userid/introduce/";
				}
				else if($userid = $this->preg('module=yellowpage&action=news&userid=%',$url)){
					return "yellowpage/$userid/news/";
				}
				else if($userid = $this->preg('module=yellowpage&action=product&userid=%',$url)){
					return "yellowpage/$userid/product/";
				}
				else if($userid = $this->preg('module=yellowpage&action=buy&userid=%',$url)){
					return "yellowpage/$userid/buy/";
				}
				else if($userid = $this->preg('module=yellowpage&action=job&userid=%',$url)){
					return "yellowpage/$userid/job/";
				}
				else if($userid = $this->preg('module=yellowpage&action=certificate&userid=%',$url)){
					return "yellowpage/$userid/certificate/";
				}
				else if($userid = $this->preg('module=yellowpage&action=guestbook&userid=%',$url)){
					return "yellowpage/$userid/guestbook/";
				}
				else if($userid = $this->preg('module=yellowpage&action=contact&userid=%',$url)){
					return "yellowpage/$userid/contact/";
				}
				else if($id = $this->preg('module=yellowpage&file=product&action=show&id=%',$url)){
					return "yellowpage/product/$id/";
				}
				else if($this->preg('module=yellowpage&file=company',$url)){
					return "yellowpage/company/";
				}
				else if($this->preg('module=yellowpage&file=product',$url)){
					return "yellowpage/product/";
				}
				else if($id = $this->preg('module=yellowpage&file=buy&action=show&id=%',$url)){
					return "yellowpage/buy/$id/";
				}
				else if($this->preg('module=yellowpage&file=buy',$url)){
					return "yellowpage/buy/";
				}
				else if($id = $this->preg('module=yellowpage&file=news&action=show&id=%',$url)){
					return "yellowpage/news/$id/";
				}
				else if($this->preg('module=yellowpage&file=news',$url)){
					return "yellowpage/news/";
				}
				else if($id = $this->preg('module=yellowpage&file=job&action=show&id=%',$url)){
					return "yellowpage/job/$id/";
				}
				else if($this->preg('module=yellowpage&file=job',$url)){
					return "yellowpage/job/";
				}
				else if($this->preg('module=yellowpage&file=myjob',$url)){
					return "yellowpage/myjob/";
				}
				/*else if($this->preg('module=yellowpage&action=news',$url)){
					return "yellowpage/news/$id/";
				}*/
				else if($this->preg('module=yellowpage&action=apply',$url)){
					return "yellowpage/apply/";
				}
				else if($this->preg('module=yellowpage&action=explore',$url)){
					return "yellowpage/explore/";
				}
				else if($this->preg('module=yellowpage',$url)){
					return "yellowpage/";
				}
				else{
					//return '?'.$url;
					return $this->other($a);
				}
			} 
			
			
		}
		function soft($a){
			extract($a);
			if($revert){
				
				if($softid = $this->preg('soft/%',$url)){
					return "module=soft&softid=$softid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($softid = $this->preg('module=soft&softid=%',$url)){
					return "soft/$softid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function tweet($a){
			extract($a);
			if($revert){
				
				
				if($tweetid = $this->preg('tweet/%/',$url)){
					if(is_numeric($tweetid)){
						return "module=tweet&tweetid=$tweetid";
					}
					else{
						return $this->other($a);
					}
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($tweetid = $this->preg('module=tweet&tweetid=%',$url)){
					return "tweet/$tweetid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function wiki($a){
			extract($a);
			if($revert){
				
				if($wikiid = $this->preg('wiki/%/',$url)){
					if(is_numeric($wikiid)){
						return "module=wiki&wikiid=$wikiid";
					}
					else{
						return $this->other($a);
					}
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($wikiid = $this->preg('module=wiki&wikiid=%',$url)){
					return "wiki/$wikiid/";
				}
				else{
					return $this->other($a);
				}
			} 
		}
		function app($a){
			extract($a);
			if($revert){
			
				if($appid = $this->preg('app/%',$url)){
					return "module=app&appid=$appid";
				}
				else if($tag = $this->preg('app/tag/%',$url)){
					return "module=app&action=tag&tag=$tag";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($appid = $this->preg('module=app&appid=%',$url)){
					return "app/$appid/";
				}
				else if($name = $this->preg('module=app&action=tag&name=',$url)){
					return "app/tag/$name/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function page($a){
			extract($a);
			if($revert){
				
				if($this->preg('page/%',$url) && !$this->preg('yellowpage/%',$url)){
					$url = str_replace('page/','',$url);
					$catdir = $args[count($args)-1];
					$parentdir = substr($url,0,-strlen($catdir)-2);
					return "module=page&catdir=$catdir&parentdir=$parentdir";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($pageid = $this->preg('module=page&pageid=%',$url)){
					return "page/$pageid/";
				}
				else if($catdir = $this->preg('module=page&catdir=%',$url)){
					return $args['parentdir'] ? "page/$args[parentdir]/$args[catdir]/" : "page/$catdir/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function sign($a){
			extract($a);
			if($revert){
			
				if($signid = $this->preg('sign/%',$url)){
					return "module=sign&signid=$signid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($signid = $this->preg('module=sign&signid=%',$url)){
					return "sign/$signid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function vote($a){
			extract($a);
			if($revert){
			
				if($voteid = $this->preg('vote/%/statistics/',$url)){
					return "module=vote&action=statistics&voteid=$voteid";
				}
				else if($subjectid = $this->preg('vote/%/',$url)){
					return "module=vote&subjectid=$subjectid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($voteid = $this->preg('module=vote&action=statistics&voteid=%',$url)){
					return "vote/$voteid/statistics/";
				}
				else if($subjectid = $this->preg('module=vote&subjectid=%',$url)){
					return "vote/$subjectid/";
				}
				else if($voteid = $this->preg('module=vote&voteid=%',$url)){
					return "vote/$voteid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function music($a){
			extract($a);
			if($revert){
			
				if($musicid = $this->preg('music/%',$url)){
					return "module=music&musicid=$musicid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($musicid = $this->preg('module=music&musicid=%',$url)){
					return "music/$musicid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function house($a){
			extract($a);
			if($revert){
			
				if($houseid = $this->preg('house/%',$url)){
					return "module=house&houseid=$houseid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($houseid = $this->preg('module=house&houseid=%',$url)){
					return "house/$houseid/";
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		function group($a){
			extract($a);
			if($revert){
				
				if($groupid = $this->preg('group/%/introduce/',$url)){
					return is_numeric($groupid) ? "module=group&action=introduce&groupid=$groupid" : "module=group&action=introduce&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/member/',$url)){
					return is_numeric($groupid) ? "module=group&action=member&groupid=$groupid" : "module=group&action=member&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/discussion/',$url)){
					return is_numeric($groupid) ? "module=group&action=discussion&groupid=$groupid" : "module=group&action=discussion&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/join/',$url)){
					return is_numeric($groupid) ? "module=group&action=join&groupid=$groupid" : "module=group&action=join&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/invite/',$url)){
					return is_numeric($groupid) ? "module=group&action=invite&groupid=$groupid" : "module=group&action=invite&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/recycle/',$url)){
					return is_numeric($groupid) ? "module=group&action=recycle&groupid=$groupid" : "module=group&action=recycle&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/statistics/',$url)){
					return is_numeric($groupid) ? "module=group&action=statistics&groupid=$groupid" : "module=group&action=statistics&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/manage/',$url)){
					return is_numeric($groupid) ? "module=group&action=manage&groupid=$groupid" : "module=group&action=manage&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/setting/',$url)){
					return is_numeric($groupid) ? "module=group&action=setting&groupid=$groupid" : "module=group&action=setting&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/event/',$url)){
					return is_numeric($groupid) ? "module=group&action=event&groupid=$groupid" : "module=group&action=event&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/link/',$url)){
					return is_numeric($groupid) ? "module=group&action=link&groupid=$groupid" : "module=group&action=link&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/domain/',$url)){
					return is_numeric($groupid) ? "module=group&action=domain&groupid=$groupid" : "module=group&action=domain&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/priv/',$url)){
					return is_numeric($groupid) ? "module=group&action=priv&groupid=$groupid" : "module=group&action=priv&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/dissolve/',$url)){
					return is_numeric($groupid) ? "module=group&action=dissolve&groupid=$groupid" : "module=group&action=dissolve&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/transfer/',$url)){
					return is_numeric($groupid) ? "module=group&action=transfer&groupid=$groupid" : "module=group&action=transfer&prefix=$groupid";
				}
				else if($groupid = $this->preg('group/%/feed/',$url)){
					return is_numeric($groupid) ? "module=group&action=feed&groupid=$groupid" : "module=group&action=feed&prefix=$groupid";
				}
				else if($this->preg('group/topic/add',$url)){
					return "module=group&file=topic&action=add";
				}
				else if($topicid = $this->preg("group/topic/%",$url)){
					return "module=group&file=topic&topicid=$topicid";
				}
				else if($this->preg('group/topic',$url)){
					return "module=group&file=topic";
				}
				else if($this->preg('group/apply',$url)){
					return "module=group&action=apply";
				}
				else if($this->preg('group/guide',$url)){
					return "module=group&action=guide";
				}
				else if($this->preg('group/mine/',$url)){
					return "module=group&action=mine";
				}
				else if($this->preg('group/disclaimer',$url)){
					return "module=group&action=disclaimer";
				}
				else if($groupid = $this->preg('group/%/',$url)){
					
					if(!in_array($groupid,array('introduce','member','discussion','join','invite','recycle','statistics','manage','setting','event','link','domain','priv','dissolve','apply','explore','disclaimer','guide','topic','transfer'))){
					
						return is_numeric($groupid) ? "module=group&groupid=$groupid" : "module=group&prefix=$groupid";
					}
					else{
					
						return "module=group&action=$groupid";
					}
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($groupid = $this->preg('module=group&groupid=%',$url)){
				
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/";
					}
					else{
						return "group/$groupid/";
					}
				}
				else if($prefix = $this->preg('module=group&prefix=%',$url)){
						return "group/$prefix/";
				}
				else if($topicid = $this->preg('module=group&file=topic&topicid=%',$url)){
					return "group/topic/$topicid/";
				}
				else if($groupid = $this->preg('module=group&action=introduce&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/introduce/";
					}
					else{
						return "group/$groupid/introduce/";
					}
				}
				else if($groupid = $this->preg('module=group&action=discussion&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/discussion/";
					}
					else{
						return "group/$groupid/discussion/";
					}
				}
				else if($groupid = $this->preg('module=group&action=member&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/member/";
					}
					else{
						return "group/$groupid/member/";
					}
				}
				else if($groupid = $this->preg('module=group&action=join&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/join/";
					}
					else{
						return "group/$groupid/join/";
					}
				}
				else if($groupid = $this->preg('module=group&action=invite&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/invite/";
					}
					else{
						return "group/$groupid/invite/";
					}
				}
				else if($groupid = $this->preg('module=group&action=recycle&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/recycle/";
					}
					else{
						return "group/$groupid/recycle/";
					}
				}
				else if($groupid = $this->preg('module=group&action=setting&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/setting/";
					}
					else{
						return "group/$groupid/setting/";
					}
				}
				else if($groupid = $this->preg('module=group&action=dissolve&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/dissolve/";
					}
					else{
						return "group/$groupid/dissolve/";
					}
				}
				else if($groupid = $this->preg('module=group&action=domain&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/domain/";
					}
					else{
						return "group/$groupid/domain/";
					}
				}
				else if($groupid = $this->preg('module=group&action=transfer&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/transfer/";
					}
					else{
						return "group/$groupid/transfer/";
					}
				}
				else if($groupid = $this->preg('module=group&action=feed&groupid=%',$url)){
					
					global $db;
					$r = $db->get_one("select `prefix` from table_group where `groupid` = $groupid");
					if($r['prefix'] != ''){
						return "group/$r[prefix]/feed/";
					}
					else{
						return "group/$groupid/feed/";
					}
				}
				else{
					return $this->other($a);
				}
			} 
			
		}
		
		function movie($a){
			extract($a);
			if($revert){
			
				if($file = $this->preg('movie/actor',$url)){
					return "module=movie&file=$file";
				}
				else if($movieid = $this->preg('movie/%',$url)){
					return "module=movie&movieid=$movieid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($movieid = $this->preg('module=movie&movieid=%',$url)){
					return "movie/$movieid/";
				}
				else{
					return $this->other($a);
				}
			} 
		}
		
		function module($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function forum($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function search($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function classad($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function digg($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function guestbook($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function message($a){
			extract($a);
			if($revert){
				if($this->preg('message/send/',$url)){
					return "module=message&action=send";
				}
				else if($this->preg('message/inbox/',$url)){
					return "module=message&file=inbox";
				}
				else if($this->preg('message/outbox/',$url)){
					return "module=message&file=outbox";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($movieid = $this->preg('module=movie&movieid=%',$url)){
					return "movie/$movieid/";
				}
				else{
					//return '?'.$url;
					return $this->other($a);
				}
				
			} 
		}
		
		function comment($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function say($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function rss($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function link($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function pay($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function order($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function mail($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function guess($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function video($a){
			extract($a);
			if($revert){
				if($this->preg('video/create/',$url)){
					return "module=video&action=create";
				}
				elseif($videoid = $this->preg('video/%',$url)){
					return "module=video&videoid=$videoid";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($videoid = $this->preg('module=video&videoid=%',$url)){
					return "video/$videoid/";
				}
				elseif($this->preg('module=video&action=create',$url)){
					return "video/create/";
				}
				else{
					return $this->other($a);
				}
			}
		}
		
		function webapp($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function wap($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function photo($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		function favorite($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		
		function joke($a){
			extract($a);
			if($revert){
				return $this->other($a);
			}
			else{
				return $this->other($a);
			}
		}
		
		
		
		
		function connect($a){
			extract($a);
			if($revert){
				if($action = $this->preg('connect/qzone/%/',$url)){
					return "module=connect&file=qzone&action=$action";
				}
				else if($action = $this->preg('connect/sinaweibo/%/',$url)){
					return "module=connect&file=sinaweibo&action=$action";
				}
				else if($action = $this->preg('connect/douban/%/',$url)){
					return "module=connect&file=douban&action=$action";
				}
				else if($action = $this->preg('connect/renren/%/',$url)){
					return "module=connect&file=renren&action=$action";
				}
				else if($action = $this->preg('connect/qqweibo/%/',$url)){
					return "module=connect&file=qqweibo&action=$action";
				}
				else if($action = $this->preg('connect/msn/%/',$url)){
					return "module=connect&file=msn&action=$action";
				}
				else{
					return $this->other($a);
				}
			}
			else{
				if($action = $this->preg('module=connect&file=qzone&action=%',$url)){
					return "connect/qzone/$action/";
				}
				else if($action = $this->preg('module=connect&file=sinaweibo&action=%',$url)){
					return "connect/sinaweibo/$action/";
				}
				else if($action = $this->preg('module=connect&file=douban&action=%',$url)){
					return "connect/douban/$action/";
				}
				else if($action = $this->preg('module=connect&file=renren&action=%',$url)){
					return "connect/renren/$action/";
				}
				else if($action = $this->preg('module=connect&file=qqweibo&action=%',$url)){
					return "connect/qqweibo/$action/";
				}
				else if($action = $this->preg('module=connect&file=msn&action=%',$url)){
					return "connect/msn/$action/";
				}
				else{
					//return '?'.$url;
					return $this->other($a);
				}
				
			} 
		}
		
		// 其他
		function other ($a){
			extract($a);
			
			if($revert){
				$return = "module=$args[0]&action=$args[1]";
		
				// 其他参数
				unset($args[0],$args[1]);
				
				$array = array();
				for($i = 0;$i <= count($args);$i = $i+2){
					$array[$args[$i]] = $args[$i+1];
				}
				
				foreach($array as $key => $value){
					if($key != '' && $value !=''){
						$param .=  "&$key=$value";
					}
				}
				$return .= $param ? $param : '';
				return $return;
			}
			else{
				parse_str($url);
				
				
				// 其他参数
				foreach($args as $k=>$v){
					if($k != 'module' && $k != 'file' && $k != 'action' && $k != 'var'){
						//$param .=  !$param ? "$k/$v" : "&"."$k/$v";
						$param .=  !$param ? "?$k=$v" : "&$k=$v";
					}
				}
				$return .= $module ? $module : '';
				$return .= $file ? '/'.$file : '';
				$return .= $action ? '/'.$action : '';
				//$return .= $param ? $param : '';
				
				return $param ? '?'.$url : $return.'/';
			} 
		}
		
		// by aming
		function preg($pregx,$str){
			if($pregx == $str) return true;
			$pregx = str_replace('%','(.*?)',$pregx);
			
			// 部分符号需要转义
			$pregx = str_replace('/','\/',$pregx);
			
			//preg_match("/(\\?".$pregx."#)/is",'?'.$str.'#',$out);
			preg_match("/(\\?".$pregx."#)/is",'?'.$str.'#',$out);
			return $out[2];
		}
		
		
		// 确定服务器开通了rewrite模块(来自互联网 http://zhidao.baidu.com/question/334255662.html)
		function apache_is_mod_rewrite(){
		
			$result = apache_get_modules();
			if(!in_array('mod_rewrite', $result)) {
				return false;
				unset($result);
			}
		}
		
		
		
	}
?>